#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class Solution {
public:
    vector<vector<int>> ret;
    vector<int> path;
    bool visited[8];

    vector<vector<int>> permuteUnique(vector<int>& nums)
    {
        sort(nums.begin(), nums.end());
        dfs(nums);
        return ret;
    }
    void dfs(vector<int>& nums)
    {
        // for (auto e : path)
        // {
        //     cout << e << ' ';
        // }
        // cout << endl;
        if (path.size() == nums.size())
        {
            ret.push_back(path);
            return;
        }
        for (int i = 0; i < nums.size(); ++i)
        {
            if ((i == 0 && visited[0] == false)
                || (i > 0 && nums[i] != nums[i - 1] && visited[i] == false)
                || (i > 0 && nums[i] == nums[i - 1] && visited[i - 1] == true))
            {
                path.push_back(nums[i]);
                visited[i] = true;
                dfs(nums);
                path.pop_back();
                visited[i] = false;
            }
        }
    }
};
int main()
{
    Solution s1;
    vector<int> nums;
    nums.push_back(1);
    nums.push_back(1);
    nums.push_back(2);
    s1.permuteUnique(nums);
	return 0;
}